home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / smaltalk.lha / smalltalk-1.1.1 / Number.st < prev    next >
Text File  |  1991-09-12  |  5KB  |  278 lines

  1. "======================================================================
  2. |
  3. |   Number Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     19 Sep 89      Converted to use real category strings.
  34. |
  35. | sbyrne     25 Apr 89      created.
  36. |
  37. "
  38.  
  39. Magnitude subclass: #Number
  40.       instanceVariableNames: ''
  41.       classVariableNames: ''
  42.       poolDictionaries: ''
  43.       category: nil.
  44.  
  45. Number comment: 
  46. 'I am an abstract class that provides operations on numbers, both floating
  47. point and integer.  I provide some generic predicates, and supply the 
  48. implicit type coercing code for binary operations.' !
  49.  
  50. !Number methodsFor: 'converting'!
  51.  
  52. degreesToRadians
  53.     ^self / 57.295779513082320876846364344191
  54. !
  55.  
  56. radiansToDegrees
  57.     ^self * 57.295779513082320876846364344191
  58. !
  59.  
  60. coerce: aNumber
  61.     self subclassResponsibility
  62. !
  63.  
  64. generality
  65.     self subclassResponsibility
  66. !
  67.  
  68. retry: aSymbol coercing: aNumber
  69.     | selfGen aNumGen |
  70.     aSymbol == #=
  71.         ifTrue: [ (aNumber isKindOf: Number) ifFalse: [ ^false ] ].
  72.     selfGen _ self generality.
  73.     aNumGen _ aNumber generality.
  74.     selfGen > aNumGen
  75.         ifTrue: [ ^self perform: aSymbol with: (self coerce: aNumber) ].
  76.     selfGen < aNumGen
  77.         ifTrue: [ ^(aNumber coerce: self) perform: aSymbol with: aNumber ].
  78.     self error: 'retry:coercing: called with arguments of the same generality' 
  79. !!
  80.  
  81.  
  82.  
  83. !Number methodsFor: 'arithmetic'!
  84.  
  85. + aNumber
  86.     self subclassResponsibility
  87. !
  88.  
  89. - aNumber
  90.     self subclassResponsibility
  91. !
  92.  
  93. * aNumber
  94.     self subclassResponsibility
  95. !
  96.  
  97. / aNumber
  98.     self subclassResponsibility
  99. !
  100.  
  101. // aNumber
  102.     self subclassResponsibility
  103. !
  104.  
  105. \\ aNumber
  106.     self subclassResponsibility
  107. !
  108.  
  109. quo: aNumber
  110.     "Return the integer quotient of dividing the receiver by aNumber with
  111.     truncation towards zero."
  112.     ^(self / aNumber) truncated
  113. !
  114.  
  115. rem: aNumber
  116.     "Return the integer remainder of dividing the receiver by aNumber with
  117.     truncation towards zero."
  118.     ^(self - ((self / aNumber) truncated * aNumber)) truncated
  119. !!
  120.  
  121.  
  122.  
  123. !Number methodsFor: 'truncation and round off'!
  124.  
  125. truncated
  126.     ^self subclassResponsibility
  127. !
  128.  
  129. truncateTo: aNumber
  130.     ^(self / aNumber) truncated * aNumber
  131. !
  132.  
  133. rounded
  134.     "Returns the integer nearest the receiver"
  135.     ^(self + 0.5) truncated
  136. !
  137.  
  138. roundTo: aNumber
  139.     ^(self / aNumber) rounded * aNumber
  140. !!
  141.  
  142.  
  143.  
  144. !Number methodsFor: 'testing'!
  145. negative
  146.     ^self < 0
  147. !
  148.  
  149. positive
  150.     ^self >= 0
  151. !
  152.  
  153. strictlyPositive
  154.     ^self > 0
  155. !
  156.  
  157. sign
  158.     "Returns the sign of the receiver."
  159.     self < 0 ifTrue: [ ^-1 ].
  160.     self > 0 ifTrue: [ ^1 ].
  161.     ^0
  162. !
  163.  
  164. even
  165.     "Returns true if self is divisible by 2"
  166.     ^self truncated even
  167. !
  168.  
  169. odd
  170.     "Returns true if self is not divisible by 2"
  171.     ^self truncated odd
  172. !!
  173.  
  174.  
  175.  
  176. !Number methodsFor: 'misc math'!
  177.  
  178. squared
  179.     ^self * self
  180. !
  181.  
  182. abs
  183.     self > 0 ifTrue: [ ^self ] ifFalse: [ ^self negated ]
  184. !
  185.  
  186. negated
  187.     ^0 - self
  188. !
  189.  
  190. sin
  191.     ^self asFloat sin
  192. !
  193.  
  194. cos
  195.     ^self asFloat cos
  196. !
  197.  
  198. tan
  199.     ^self asFloat tan
  200. !
  201.  
  202. arcSin
  203.     ^self asFloat arcSin
  204. !
  205.  
  206. arcCos
  207.     ^self asFloat arcCos
  208. !
  209.  
  210. arcTan
  211.     ^self asFloat arcTan
  212. !
  213.  
  214. sqrt
  215.     ^self asFloat sqrt
  216. !
  217.  
  218. exp
  219.     ^self asFloat exp
  220. !
  221.  
  222. ln
  223.     ^self asFloat ln
  224. !
  225.  
  226. log: aNumber
  227.     "return log base aNumber of the receiver"
  228.     ^self ln / aNumber ln
  229. !
  230.  
  231. floorLog: radix
  232.     ^(self log: radix) floor
  233. !
  234.  
  235. raisedToInteger: anInteger
  236.     "Return self raised to anInteger power"
  237.     | result |
  238.     result _ self coerce: 1.
  239.     anInteger timesRepeat: [ result _ result * self ].
  240.     ^result
  241. !!
  242.  
  243.  
  244.  
  245. !Number methodsFor: 'Interval iterators'!
  246.  
  247. to: stop
  248.     ^Interval from: self to: stop
  249. !
  250.  
  251. to: stop by: step
  252.     ^Interval from: self to: stop by: step
  253. !
  254.  
  255. to: stop by: step do: aBlock
  256.     | i |
  257.     i _ self.
  258.     step > 0
  259.         ifTrue: [
  260.             [ i <= stop ]
  261.             whileTrue: [ aBlock value: i.
  262.                      i _ i + step ]
  263.         ]
  264.     ifFalse: [
  265.             [ i >= stop ]
  266.             whileTrue: [ aBlock value: i.
  267.                      i _ i + step ]
  268.     ]
  269.     " ??? What's the return value? "
  270. !
  271.  
  272. to: stop do: aBlock
  273.     ^self to: stop by: 1 do: aBlock
  274. !!
  275.  
  276.